<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('减持记录') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('证券账号')" prop="securityId">
        <el-select v-model="queryParams.securityId">
          <el-option
            v-for="dict in securityOptions"
            :key="dict.id"
            :label="dict.securityLabel"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('减持日期')" prop="subDate">
        <DatePicker v-model="dataRelease" type="daterange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" tooltip-effect="darkTable" :data="listedShareSubDetailList" :summary-method="getSummaries" show-summary border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('证券账号')" align="center" prop="securityAccount" min-width="180" show-overflow-tooltip />
      <el-table-column :label="$t('证券公司')" align="center" prop="securityName" min-width="180" show-overflow-tooltip />
      <el-table-column :label="$t('减持日期')" align="center" prop="subDate" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('减持总股数(万股)')" align="center" prop="totalSubShare" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.totalSubShare) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('减持总金额') + $t('金额单位')" align="center" prop="totalSubAmount" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.totalSubAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('减持均价(元/股)')" align="center" prop="subPrice" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.subPrice) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('减持比例(%)')" align="center" prop="subShareRate" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.subShareRate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('减持税费') + $t('金额单位')" align="center" prop="subTaxe" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.subTaxe, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('减持费率(%)')" align="center" prop="subFeeRate" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.subFeeRate) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listShareSubDetail } from '@/api/deal/listed/shareSub/listedShareSubDetail'
import { listListedSecurity } from '@/api/deal/listed/security/listedSecurity'
import { moneyFormat } from '@/utils/ruoyi'

export default {
  components: {
  },
  props: {
    listedId: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 上市项目-减持记录表格数据
      listedShareSubDetailList: [],
      // 减持日期时间范围
      dataRelease: [],
      // 获取证券账户options
      securityOptions: [],
      // 查询参数
      queryParams: {
        listedId: this.listedId,
        securityId: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  watch: {
    listedId: {
      handler(val) {
        this.queryParams.listedId = val
        this.getList()
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    // 获取证券账户options
    listListedSecurity(this.listedQueryParam).then(response => {
      this.securityOptions = response.rows
    })
    this.getList()
  },
  methods: {
    /** 查询上市项目-减持记录列表 */
    getList() {
      this.loading = true
      if (this.dataRelease != null && this.dataRelease.length > 1) {
        this.queryParams.startDate = this.dataRelease[0]
        this.queryParams.endDate = this.dataRelease[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      listShareSubDetail(this.queryParams).then(response => {
        this.listedShareSubDetailList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        listedId: undefined,
        dealName: undefined,
        stockCode: undefined,
        shareId: undefined,
        securityId: undefined,
        securityName: undefined,
        securityAccount: undefined,
        totalSubAmount: undefined,
        totalSubShare: undefined,
        subPrice: undefined,
        subTaxe: undefined,
        subFeeRate: undefined,
        subShareRate: undefined,
        tradingWay: undefined,
        subDate: undefined,
        opponentId: undefined,
        opponentName: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dataRelease = []
      this.queryHandle()
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          // 减持总股数(万股),由于数据库存单位是个位，不是万位，所以不格式万元的
          if (column.property === 'totalSubShare') {
            sums[index] = moneyFormat(sums[index])
          } else if (column.property === 'totalSubAmount') { // 减持总金额
            sums[index] = moneyFormat(sums[index], 'w')
          } else if (column.property === 'subShareRate') { // 减持比例（%）
            sums[index] = moneyFormat(sums[index])
          } else if (column.property === 'subTaxe') { // 减持税费（万元）
            sums[index] = moneyFormat(sums[index], 'w')
          } else {
            sums[index] = ''
          }
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>
